package com.v5.ik;

import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zy on 2016/7/7.
 */
public abstract class VolleyHelp {
    public void getData(int method, String url) {
        Log.i("qwe", url);
        StringRequest stringRequest = new StringRequest(method, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.i("qwe", "success");
//                try {
//                    String dataString = new String(s.getBytes("ISO-8859-1"), "utf-8");
                    onJsonData(s);
                    Log.i("qwe", "json:" + s);
//                } catch (UnsupportedEncodingException e) {
//                    e.printStackTrace();
//                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("qwe", "err");
                onError(volleyError);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return getMyHeaders(super.getHeaders());
            }

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return getMyParams();
            }
        };
        MyApp.getMyApp().getRequestQueue().add(stringRequest);
    }

    public abstract void onJsonData(String s);

    public Map<String, String> getMyHeaders(Map<String, String> map) {
        return map;
    }

    public Map<String, String> getMyParams() {
        return new HashMap<>();
    }

    public void onError(VolleyError volleyError) {
    }
}
